import java.io.{FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream}

import com.esotericsoftware.kryo.io.{Input, Output}
import com.twitter.chill.KryoSerializer
import org.apache.commons.lang3.RandomStringUtils
import org.apache.hadoop.util.bloom.{BloomFilter, Key}
import org.apache.spark.SparkConf
import org.apache.spark.serializer.KryoSerializer

object Bloom {
  def main(args: Array[String]): Unit = {
    val filter: BloomFilter = new BloomFilter(1000000000, 5, 1)
    for(i<- 1 to 10000000){
      filter.add(new Key(RandomStringUtils.randomAlphanumeric(3,8).getBytes))
    }

    val conf = new SparkConf()
    conf.registerKryoClasses(Array(classOf[BloomFilter]))

    val kryo = new KryoSerializer(conf).newKryo()

    kryo.writeClassAndObject(new Output(new FileOutputStream("d:/filter.obj")),filter )

    val f:BloomFilter= kryo.readClassAndObject(new Input(new FileInputStream("d:/filter.obj"))).asInstanceOf[BloomFilter]
    println(f.membershipTest(new Key("ccc".getBytes())))
  }

}
